home *** CD-ROM | disk | FTP | other *** search
/ SPACE 1 / SPACE - Library 1 - Volume 1.iso / program / 441 / gemfxm12 / memfind.c next >
C/C++ Source or Header  |  1990-11-23  |  13KB  |  345 lines

  1.  
  2. /**************************************************************************
  3.  *
  4.  * MEMFIND.C - An example program for AESFAST which uses just dialogs.
  5.  *
  6.  *  Public Domain example program by Ian Lepore.
  7.  *
  8.  *  This is distributed as an example of how to write a simple program using
  9.  *  my AESFAST public domain GEM bindings.  This example uses a few of
  10.  *  the nifty utilities from AESFAST, but it's pretty much straightforward
  11.  *  dialog-handling code.
  12.  *
  13.  *  This beast may be marginally useful beyond its value as an example.
  14.  *  It will find and report on the 5 biggest blocks of free memory in 
  15.  *  the system, giving a somewhat more acurate view than programs which
  16.  *  report only the largest free block.
  17.  *
  18.  *  This code is pretty heavily commented.  Please excuse me if some of 
  19.  *  the comments seem obvious, but I figure the audience for this will 
  20.  *  include both beginning C programmers, and old-timers who just need to
  21.  *  see how my bindings work as opposed to other bindings.
  22.  *
  23.  *************************************************************************/
  24.  
  25. #include <gemfast.h>
  26. #include <osbind.h>
  27. #include "memfind.h"
  28.  
  29. #ifndef TRUE
  30. #define TRUE  1
  31. #define FALSE 0
  32. #endif
  33.  
  34. #define NO_RSRC   -2783        /* a random number */
  35.  
  36. /**************************************************************************
  37.  *
  38.  * global vars
  39.  *
  40.  *************************************************************************/
  41.  
  42. typedef struct {
  43.         char *pmem;
  44.         long lmem;
  45.         int  objlink;
  46.         char displaystr[25];
  47.         } MEM_BLOCK;
  48.  
  49. #define NUM_BLOCKS 5
  50.  
  51. MEM_BLOCK mem_ctl[NUM_BLOCKS];
  52.  
  53. OBJECT    *maintree;
  54.  
  55. char map_template[] = "$%06lx    %4dk";    /* template for printf() */
  56.  
  57. char no_rsrc_alert[] = "[3][ | Can't open/load RSC file! | ][ Fatal ]";
  58.  
  59. /**************************************************************************
  60.  *
  61.  * prg_exit - Cleanup and terminate.
  62.  *  If the exit code is our magic number NO_RSRC (indicating that we're
  63.  *  exiting because the resource load failed), we skip the rsrc_free call.
  64.  *
  65.  *************************************************************************/
  66.  
  67. void
  68. prg_exit(code)
  69.         int code;
  70. {
  71.         if (code != NO_RSRC) {
  72.                 rsrc_free();
  73.         }
  74.  
  75.         appl_exit();
  76.         Pterm(code);
  77. }
  78.  
  79. /**************************************************************************
  80.  *
  81.  * prg_init.
  82.  *
  83.  *  This routine does mundance AES init stuff, and makes the connections
  84.  *  between the string objects in the resource tree and the elements in
  85.  *  our memory control array.
  86.  *
  87.  *  The connections concept is based upon accessing arrays of strings in
  88.  *  a dialog box without knowing the actual object indicies (names) of
  89.  *  the string objects.  (Some discussion of this can also be found in 
  90.  *  the MINICOLR accessory example code).  I'll be the first to admit
  91.  *  that this technique is overkill for this little program, since only
  92.  *  five text strings are involved.  On the other hand, you can increase
  93.  *  the number of displayed memory fragments simply by going into the 
  94.  *  resource file and making a few more copies of the display strings, 
  95.  *  then coming into this source code and increasing NUM_BLOCKS to match
  96.  *  the new number of display strings in the resource file.  No other 
  97.  *  changes are necessary, and *that's* something you can't say about
  98.  *  'normal' GEM coding techniques.
  99.  *
  100.  *   What this technique really does is isolate the location of the objects
  101.  *   in the RSC file from the program code.  Thus, a hacker-type can munge
  102.  *   up the resource file and the program will still run correctly, even
  103.  *   if objects are added, deleted, moved, or sorted.
  104.  *
  105.  *   In this implementation, I've set an 'extended object type' on each
  106.  *   of the display string objects, using my resource editor.  I chose
  107.  *   a value of '1' for the extended type, but this is completely 
  108.  *   arbitrary.
  109.  *
  110.  *   Just for grins, I'll list here the code I would have used if I hadn't
  111.  *   implemented the location-independant connections concept.  First, I'll
  112.  *   explain my standard for naming objects...
  113.  *       nnnnttxx
  114.  *       ||||||++--- 2 char arbitrary id (my object name)
  115.  *       ||||++----- object type (see list below)
  116.  *       ++++------- name of the tree holding the object
  117.  *     Object types for naming standards are:
  118.  *        BX  - Button (eXit)
  119.  *        BR  - Button (Radio) (also used for radio boxchars)
  120.  *        ST  - STring
  121.  *        TX  - TeXt (display only)
  122.  *        TE  - Text (Editable)
  123.  *        PB  - Parent Box (usually invisible, parent for radio buttons)
  124.  *        TREE- Special-case name, indicates a root (R_TREE) objct.
  125.  *     Thus, using this standard, you might have:
  126.  *        MAINBXOK - Exit button 'OK' in main dialog box.
  127.  *        DEVSBRDA - Radio Button for drive A in device selection dialog.
  128.  *        MAINSTM1 - Display string M1 in main dialog.
  129.  *
  130.  *   Anyway, enough standards.  You don't have to use my standard, but
  131.  *   I'd advise you to use *some* kind of naming standard that ties object
  132.  *   names to the trees they live in, if you want to maintain your sanity.
  133.  *
  134.  *   So, IF I had done this program in the 'normal' way, the loop below
  135.  *   which does the 'connections' works would be replaced by the following:
  136.  *
  137.  *      rsc_sstrings(maintree,
  138.  *                      MAINSTM1,mem_ctl[0].displaystr,
  139.  *                      MAINSTM2,mem_ctl[1].displaystr,
  140.  *                      MAINSTM3,mem_ctl[2].displaystr,
  141.  *                      MAINSTM4,mem_ctl[3].displaystr,
  142.  *                      MAINSTM5,mem_ctl[4].displaystr,
  143.  *                      -1);
  144.  *
  145.  *   The rsc_sstrings routine is an AESFAST library routine that sets the
  146.  *   ob_spec pointers for 1-n strings within a dialog tree.
  147.  *************************************************************************/
  148.  
  149. void
  150. prg_init()
  151. {
  152.         int                dmy;
  153.         register int       objcounter;
  154.         register int       strcounter;
  155.         register OBJECT    *ptree;
  156.         register MEM_BLOCK *pblock;
  157.         
  158. /*
  159.  * call AES init, then try to load the resource file.  if the RSC load
  160.  * fails, go whine at the user and exit.
  161.  */
  162.  
  163.         appl_init();
  164.         
  165.         if (!rsrc_load("MEMFIND.RSC")) {
  166.                 form_alert(no_rsrc_alert);
  167.                 prg_exit(NO_RSRC);
  168.         }
  169.  
  170. /*
  171.  * get the address of the dialog tree and center the d-box.
  172.  */
  173.      
  174.         rsrc_gaddr(R_TREE, MAINTREE, &maintree);
  175.         form_center(maintree, &dmy, &dmy, &dmy, &dmy);
  176.    
  177. /*
  178.  * connect up the links between the memory control structures and the
  179.  * object structures... (see notes above).
  180.  *
  181.  * go through the object tree, and each time we encounter an object
  182.  * with the right extended object type, connect that object's ob_spec
  183.  * pointer to the display string in the memory control structure, and
  184.  * make note of the object's index in the objlink field of the memory
  185.  * control structure.  normally, only the ob_spec link would be made,
  186.  * but for our application we need to be able to set an object's flags
  187.  * to HIDETREE if we discover an empty block in the memory control struct.
  188.  *
  189.  * the loop control here will stop if we run out of memory control blocks
  190.  * or if we run out of objects in the tree.  this means that if someone
  191.  * munges up the resource file and removes some of our string objects,
  192.  * then some memory blocks won't be displayed, but at least nothing dies.
  193.  */     
  194.         objcounter = strcounter = 0;
  195.         do      {
  196.                 ptree = &maintree[objcounter];
  197.                 if (1 == (ptree->ob_type >> 8)) {
  198.                         pblock = &mem_ctl[strcounter];
  199.                         ptree->ob_spec  = (long)pblock->displaystr;
  200.                         pblock->objlink = objcounter;
  201.                         strcounter++;
  202.                 }
  203.                 objcounter++;
  204.         } while ( (strcounter < NUM_BLOCKS) &&
  205.                   (!(ptree->ob_flags & LASTOB)) );
  206.  
  207. /* 
  208.  * init all done, change the mouse from a busy-bee to an arrow.
  209.  */
  210.         graf_mouse(ARROW, 0L);
  211. }
  212.  
  213. /**************************************************************************
  214.  *
  215.  * dial_do - Generic dialog handler.
  216.  *
  217.  *  This routine handles dialog interaction for any dialog box.  If the
  218.  *  d-box has editable text objects, pass this routine the object index
  219.  *  of the first text object to edit.  If there are no edit objects, just
  220.  *  pass a zero.  This routine assumes that a form_center call has already
  221.  *  been done (I always do the center calls during init processing, since
  222.  *  it makes no sense to center the same dialog box multiple times if the
  223.  *  dialog is invoked multiple times).  This routine does NOT do the 
  224.  *  annoying graphics calls (FMD_GROW and FMD_SHRINK).  You can put them
  225.  *  in if you have the patience to watch little lines zoom around on the
  226.  *  screen.
  227.  *
  228.  *  Since form_center is the usual source of info on the dialog box's 
  229.  *  clipping rectangle, and we don't do a form_center, we call the AESFAST
  230.  *  utility routine 'objcl_calc()' to calculate a clipping rectangle for
  231.  *  the dialog box tree.  This is faster than the form_center call anyway.
  232.  *
  233.  *  Another library routine, 'objst_change' is used to de-select the exit
  234.  *  object.  The object is not changed visually on the screen (it looks
  235.  *  ugly if you do it that way), but will display as non-selected the next
  236.  *  time the dialog box is displayed.  It should be noted that it is 
  237.  *  probably faster to de-select the object with a line of code like:
  238.  *    tree[exitobj].ob_state &= ~SELECTED;
  239.  *  but I think the function call is more readable.
  240.  *
  241.  *  This routine returns the index of the object used to exit the dialog.
  242.  *************************************************************************/
  243.  
  244. int
  245. dial_do(tree, startedit)
  246.         register OBJECT *tree;
  247.         int             startedit;
  248. {
  249.         int   exitobj;
  250.         GRECT cliprect; 
  251.        
  252.         objcl_calc(maintree, R_TREE, &cliprect, 0L);
  253.         
  254.         form_dial(FMD_START, 0,0,0,0, cliprect);
  255.  
  256.         objc_draw(tree, R_TREE, MAX_DEPTH, cliprect);
  257.  
  258.         exitobj = form_do(tree, startedit);
  259.  
  260.         objst_change(tree, exitobj, ~SELECTED, FALSE);
  261.  
  262.         form_dial(FMD_FINISH, 0,0,0,0, cliprect);
  263.         
  264.         return exitobj;
  265. }
  266.  
  267. /**************************************************************************
  268.  *
  269.  * main routine - Call init routine, map out the memory blocks, display
  270.  *  the map, then call the exit routine.
  271.  *
  272.  *************************************************************************/
  273.  
  274. main()
  275. {
  276.         register int       counter;
  277.         register int       kbytes;
  278.         register MEM_BLOCK *pblock;
  279.  
  280. /*
  281.  * go do GEM initialization...
  282.  */
  283.  
  284.         prg_init();
  285.  
  286. /*
  287.  * find the biggest memory blocks available in the system.  for each
  288.  * loop iteration, ask TOS what the biggest available memory block is.
  289.  * if TOS returns a zero, we've found all available fragments already,
  290.  * so set the corresponding object's flags to HIDETREE so it won't 
  291.  * display anything.  (if the objlink field in the memory control struct
  292.  * points to object 0 (R_TREE), we don't set the HIDE flag, because the
  293.  * whole dialog box would be hidden.  this is only to handle the case
  294.  * where we ran out of strings in the object tree before we ran out of
  295.  * memory control blocks when we were setting the links in the init 
  296.  * routine. any links that didn't get set would be 0.)
  297.  *
  298.  * when a block of memory is found, allocate it, and save its pointer so
  299.  * we can free it later.  format the display string to contain the memory
  300.  * block's address and size, then continue with the next loop iteration. 
  301.  */
  302.  
  303.         for (counter = 0; counter < NUM_BLOCKS; counter++) {
  304.         
  305.                 pblock = &mem_ctl[counter];
  306.                 pblock->lmem = Malloc(-1L);
  307.  
  308.                 if (pblock->lmem > 0L) {
  309.                         kbytes = (int)(pblock->lmem / 1024);
  310.                         
  311.                         if (kbytes == 0)        /* fake a small block   */
  312.                                 kbytes = 1;     /* into looking like 1k */
  313.  
  314.                         pblock->pmem = (char *)Malloc(pblock->lmem);
  315.  
  316.                         sprintf(pblock->displaystr, map_template, 
  317.                                   pblock->pmem, kbytes );
  318.                 } 
  319.                 else { /* memory-block size == 0 */
  320.                         if (pblock->objlink != R_TREE) {
  321.                                 objfl_change(maintree, pblock->objlink, 
  322.                                                 HIDETREE, FALSE);
  323.                         }
  324.                 }
  325.         }
  326.  
  327. /* 
  328.  * we've built and formatted the memory map, so now we can free all
  329.  * the blocks we allocated.
  330.  */
  331.  
  332.         for (counter = 0; counter < NUM_BLOCKS; counter++) {
  333.                 Mfree(mem_ctl[counter].pmem);
  334.         }
  335.  
  336. /*
  337.  * now display the results of the mapping, and exit.
  338.  */
  339.  
  340.         dial_do(maintree,0);
  341.         
  342.         prg_exit(0);
  343. }
  344.  
  345.